

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on  
 
global path_data_processed "\data\processed"
global path_output "\data\output"

 
clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 
 gen updating2 =  posterior_f - posterior
  
loc experiments "z_follownews_f z_follownews_f z_follownews_f"


gen ind_cyclical=1-health_educ-ind_other
tab ind_cyclical, m
gen ind_noncyclical=1-ind_cyclical


replace D_log_spending_f=. if D_log_spending_f!=D_log_spending_f_w02
replace z_D_log_spending_f=. if D_log_spending_f!=D_log_spending_f_w02


foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1
global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

    loc ++columns
    qui eststo col`columns': reg x y

}

restore


/* Statistics */

loc colnum = 1
loc colnames ""
 
 gen t_ind_cyclical = ind_cyclical*treatment
 
 
     reg z_follownews_f posterior $controls  if atleast_bachelor, r
     reg z_follownews_f posterior $controls  if atleast_bachelor==0, r


     reg z_follownews_f   treatment $controls if atleast_bachelor, r first
     reg z_follownews_f   treatment $controls if atleast_bachelor==0, r first


     reg z_follownews_f   treatment $controls if ind_cyclical, r first
     reg z_follownews_f   treatment $controls if ind_cyclical==0, r first
	 
	 reg z_follownews_f t_ind_cyclical  treatment ind_cyclical $controls , r first

***OLS


    qui reg z_follownews_f posterior $controls , r
    sigstar posterior, prec(3)
    estadd loc thisstat4 = "`r(bstar)'": col1
    estadd loc thisstat5 = "`r(sestar)'": col1
	*qui sum `choice'
    *estadd loc thisstat7 = r(N): col`colnum'

	
***IV
	
    qui ivreg2 z_follownews_f (posterior = treatment) $controls , r first
	mat firststage = e(first)
	mat r2=e(r2)
	sigstar posterior, prec(3)
    estadd loc thisstat10 = "`r(bstar)'": col1
    estadd loc thisstat11 = "`r(sestar)'": col1
	qui sum z_follownews_f
    estadd loc thisstat13 = r(N): col`colnum'	
	*estadd loc thisstat14 = string(r2[1,1], "%9.2f"): col`colnum'	
	*estadd loc thisstat15 = string(r(mean), "%9.2f"): col1
	*estadd loc thisstat16 = string(r(sd), "%9.2f"): col1
	estadd loc thisstat15 = string(firststage[8,1], "%9.2f"): col1
			
			
			
	reg updating_f	z_follownews_f $controls
sigstar z_follownews_f, prec(3)
    estadd loc thisstat21 = "`r(bstar)'": col2
    estadd loc thisstat22 = "`r(sestar)'": col2
	qui sum updating_f
    estadd loc thisstat24 = r(N): col2
	
		
	reg updating2	z_follownews_f $controls
sigstar z_follownews_f, prec(3)
    estadd loc thisstat21 = "`r(bstar)'": col3
    estadd loc thisstat22 = "`r(sestar)'": col3
	qui sum updating2  
    estadd loc thisstat24 = r(N): col3
	
		
	
	
	
	
loc rowlabels " " " "{\bf Panel A: Effect of}" "{\bf recession expectations (OLS)}" "Posterior: Recession" " " " " "\hline" "{\bf Panel B: Effect of}" "{\bf recession expectations (IV)}" "Posterior: Recession" "  " " " "Observations" " " "First stage F-stat" " " "\hline" "{\bf Panel C: Effect of}" "{\bf news consumption (OLS)}"  " "  "Follow news" "(follow-up)" " " "Observations"  "
loc rowstats ""

forval i = 1/24 {
    loc rowstats "`rowstats' thisstat`i'"
}



esttab * using "follownews.tex", replace cells(none) booktabs nonotes nomtitles compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
    mgroups("\shortstack{Follow\\news\\(follow-up)}" "\shortstack{Updating\\recession\\expectations (a)}" "\shortstack{Updating\\recession\\expectations (b)}", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

eststo clear	

